﻿// JavaScript Document
function checkUser(o){
	if(o.value != ""){
		if(o.value.trim() == "" || o.value.indexOf(' ') > -1){
			$("userNameMsg").innerHTML = "用户名不能含有空格!";
			$("userNameMsg").style.display = "";
			$("userNameMsg").className="inputCheck_waring";
			return;
		}
		
		var result = retFlag(o.value);
		if(result != "true"){
			$("userNameMsg").innerHTML = result;
			$("userNameMsg").style.display = "";
			$("userNameMsg").className="inputCheck_waring";
			return;
		}else{
			$("userNameMsg").innerHTML = "...检测中";
			$("userNameMsg").style.display = "";
			$("userNameMsg").className="inputChecking";
			var checkUserName = new AjaxProxy();
			checkUserName.addParm("cLoginname",o.value);
			checkUserName.invoke("register.checkUser",true,checkUserName,function(obj){
				var count = obj.getString("count");
				if(count > 0){
					$("userNameMsg").innerHTML = "用户名已经被注册!";
					$("userNameMsg").style.display = "";
					$("userNameMsg").className="inputCheck_waring";
					return;
				}
				$("userNameMsg").innerHTML = "恭喜你,该用户名可以使用!";
				$("userNameMsg").style.display = "";
				$("userNameMsg").className="inputCheck_success";										
			});
			
		}
		
	}
}

function passCheck(o){
	if(o.value != ""){
		if(o.value.length < 6) {
			$("userPassMsg").innerHTML = "密码长度不够,至少6位!";
			$("userPassMsg").style.display = "";
			$("userPassMsg").className="inputCheck_waring";
			return;
		}else{
			$("userPassMsg").innerHTML = "密码设置正确!";
			$("userPassMsg").style.display = "";
			$("userPassMsg").className="inputCheck_success";
		}
	}
}

function passCheck1(o){
	if($("cLoginpass").value != ""){
		if($("cLoginpass").value != o.value){
			$("userPassMsg1").innerHTML = "确认密码不一致!";
			$("userPassMsg1").style.display = "";
			$("userPassMsg1").className="inputCheck_waring";
			return;
		}else{
			$("userPassMsg1").innerHTML = "确认密码正确!";
			$("userPassMsg1").style.display = "";
			$("userPassMsg1").className="inputCheck_success";
		}
	}
}

function emailCheck(o){
	if(o.value != ""){
		var email = checkEmail(o.value);
		if(email != "true"){
			$("userEmail").innerHTML = email;
			$("userEmail").style.display = "";
			$("userEmail").className="inputCheck_waring";
			return;
		}else{
			$("userEmail").innerHTML = "...检测中";
			$("userEmail").style.display = "";
			$("userEmail").className="inputChecking";
			var checkUserEmail = new AjaxProxy();
			checkUserEmail.addParm("cEmail",o.value);
			checkUserEmail.invoke("register.checkUserEmail",true,checkUserEmail,function(obj){
				var count = obj.getString("count");
				if(count > 0) {
					$("userEmail").innerHTML = "该邮箱已被其它人注册!";
					$("userEmail").style.display = "";
					$("userEmail").className="inputCheck_waring";
					return;
				}
				$("userEmail").innerHTML = "恭喜你,该邮箱可以使用!";
				$("userEmail").style.display = "";
				$("userEmail").className="inputCheck_success";												 
			});
			
		}
	}
}

function yzmCheck(o){
	if(o.value == ""){
		$("userYzm").innerHTML = "请输入验证码!<BR>";
		$("userYzm").style.display = "";
		$("userYzm").className="inputCheck_waring";
	}else{
		$("userYzm").style.display = "none";
	}
}

function register(){
	if($("cLogin").value == "") {
		$("cLogin").focus();
		$("userNameMsg").innerHTML = "请输入用户名!";
		$("userNameMsg").style.display = "";
		$("userNameMsg").className="inputCheck_waring";
		return;
	}else{
		if($("userNameMsg").className == "inputCheck_waring" || $("userNameMsg").className == ""){
			$("cLogin").focus();
			return;
		}
	}
	
	if($("cLoginpass").value == "") {
		$("cLoginpass").focus();
		$("userPassMsg").innerHTML = "请输入密码!";
		$("userPassMsg").style.display = "";
		$("userPassMsg").className="inputCheck_waring";
		return;
	}else{
		if($("userPassMsg").className == "inputCheck_waring" || $("userPassMsg").className == ""){
			$("cLoginpass").focus();
			return;
		}
	}
	
	if($("cLoginpass1").value == "") {
		$("cLoginpass1").focus();
		$("userPassMsg1").innerHTML = "请输入确认密码!";
		$("userPassMsg1").style.display = "";
		$("userPassMsg1").className="inputCheck_waring";
		return;
	}else{
		if($("userPassMsg1").className == "inputCheck_waring" || $("userPassMsg1").className == ""){
			$("cLoginpass1").focus();
			return;
		}
	}
	
	if($("cEmail").value == "") {
		$("cEmail").focus();
		$("userEmail").innerHTML = "请输入邮箱地址!";
		$("userEmail").style.display = "";
		$("userEmail").className="inputCheck_waring";
		return;
	}else{
		if($("userEmail").className == "inputCheck_waring"  || $("userEmail").className == ""){
			$("cEmail").focus();
			return;
		}
	}
	
	if($("cYzm").value == "") {
		$("cYzm").focus();
		$("userYzm").innerHTML = "请输入验证码!<BR>";
		$("userYzm").style.display = "";
		$("userYzm").className="inputCheck_waring";
		return;
	}else{
		$("userYzm").style.display = "none";
	}
	
	if(!$("chkService").checked){
		alert("必须同意注册条款才可以注册!");
		return;
	}
	
	var userRegister = new AjaxProxy();
	userRegister.addParm("cLogin",$("cLogin").value);
	userRegister.addParm("cLoginpass",$("cLoginpass").value);
	userRegister.addParm("cEmail",$("cEmail").value);
	userRegister.addParm("cYzm",$("cYzm").value);
	userRegister.invoke("register.userRegister",false);
	var flag = userRegister.getString("PN_RET");
	if(flag != 0){
		$("imgYzm").src = "/checkCode?key="+new Date().getUTCMilliseconds();
		if(flag == -1) {
			alert("注册失败!请从新注册!");
		} else if(flag == 1){
			$("userNameMsg").innerHTML = userRegister.getString("PC_MSG");
			$("userNameMsg").style.display = "";
			$("userNameMsg").className="inputCheck_waring";	
		} else if(flag == 2) {
			$("userEmail").innerHTML = userRegister.getString("PC_MSG");
			$("userEmail").style.display = "";
			$("userEmail").className="inputCheck_waring";
		} else if(flag == 3){
			$("userYzm").innerHTML = userRegister.getString("PC_MSG")+"<BR>";
			$("userYzm").style.display = "";
			$("userYzm").className="inputCheck_waring";
		}
	}else{
		//alert("恭喜你,用户注册成功!请返回邮箱激活用户,加入好多图!");
		window.location.replace("success.jsp");
	}
}

function retFlag(username){
	switch( isUsername( username ) ){
        case 0: 
            return "true";
        case 1: {
            errorUsername = "用户名不能以数字开头!";
            //用户名不能以数字开头
            return errorUsername;
        }
        case 2: {
            errorUsername = "用户名字符长度有误!";
            //合法长度为6-20个字符
            return errorUsername ;
        }
        case 3: {
            errorUsername = "用户名含有非法字符!";
            return errorUsername ;
        }
        case 4: {
            errorUsername = "用户名不能以_开头!";
            //用户名只能包含_,英文字母，数字
            return errorUsername;
        }
    }

}

function isUsername( username ){
	if(! /^[\w_]*$/.test( username ) ){
        return 3;
    }
    if( /^\d.*$/.test( username ) ){
        return 1;
    }
    if(! /^.{6,16}$/.test( username ) ){
        return 2;
    }
    if(! /^([a-z]|[A-Z])[\w_]{5,19}$/.test( username ) ){
        return 4;
    }
    return 0;
}

function checkEmail(email){
    var errorEmailStr = "";
    //设置emai的错误信息

    var emailReg = /([0-9a-zA-Z_-]+)@([0-9a-zA-Z_-]+)\.([0-9a-zA-Z_-]+)/;
    if(!emailReg.test(email)){
        errorEmailStr = "Email地址不合法！\n";
    }else{
		errorEmailStr = "true";
	}

    return errorEmailStr;
}

function $(){
	return document.getElementById(arguments[0]);
}

String.prototype.trim = function() { 
	return this.replace(/(^\s*)|(\s*$)/g, "");
} 